package sat.component;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.MTLine;
import org.mt4j.components.visibleComponents.shapes.MTPolygon;
import org.mt4j.input.gestureAction.TapAndHoldVisualizer;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapAndHoldProcessor.TapAndHoldEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapAndHoldProcessor.TapAndHoldProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

public class LinkAssociation extends MTLine{

	private MTPolygon entity1;
	private MTPolygon entity2;
	private LinkAssociation instance;
	private double degPrec = 0;

	public double getDegPrec() {
		return degPrec;
	}

	public void setDegPrec(double degPrec) {
		this.degPrec = degPrec;
	}
	
	public MTPolygon getEntity1() {
		return entity1;
	}
	public Vertex getEntity1Centre(){
		return new Vertex(entity1.getCenterPointGlobal().x,entity1.getCenterPointGlobal().y);
	}

	public MTPolygon getEntity2() {
		return entity2;
	}
	public Vertex getEntity2Centre(){
		return new Vertex(entity2.getCenterPointGlobal().x,entity2.getCenterPointGlobal().y);
	}
	public double getCurrentDegree(){
		float a = entity1.getCenterPointGlobal().x;
		float b = entity1.getCenterPointGlobal().y;
		float c = entity2.getCenterPointGlobal().x;
		float d = entity2.getCenterPointGlobal().y;
		return Math.atan((d-b)/(c-a));
	}
	
	public LinkAssociation(MTApplication mtApplication, MTPolygon a, MTPolygon b) {
		super(mtApplication, a.getCenterPointGlobal().x, a.getCenterPointGlobal().y, 
				b.getCenterPointGlobal().x, b.getCenterPointGlobal().y);
		// TODO Auto-generated constructor stub
		entity1 = a;
		entity2 = b;
		instance = this;

		this.setStrokeColor(MTColor.BLACK);
		this.setStrokeWeight(5);
		this.translate(new Vector3D(0,0,-1)); // put the link under the class

		this.removeAllGestureEventListeners();
		this.addGestureListener(TapAndHoldProcessor.class, new TapAndHoldVisualizer(mtApplication, this));
		this.registerInputProcessor(new TapAndHoldProcessor(mtApplication,1500));
		this.addGestureListener(TapAndHoldProcessor.class, new IGestureEventListener() {
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				TapAndHoldEvent e = (TapAndHoldEvent)ge;
				if (e.getId() == TapAndHoldEvent.GESTURE_ENDED && e.isHoldComplete()){
					System.out.println("start");
					e.stopPropagation();
					instance.destroy();
				}
				return false;
			}
		});
	}

}
